/* 
 * Author: tangchao@jd.com
 * Date: 2013-05-28
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "global.h"

static
void
my_help(void)
{
  printf("Usage:\n"
         "[command] -t 100 -h 202.106.0.20 -u scott -p tigger -d test -P 3306 -c 10000\n"
         "-t\tthread num\n"
         "-h\thostname\n"
         "-u\tuser\n"
         "-p\tpassword\n"
         "-d\tdatabase\n"
         "-P\tport\n"
         "-c\tquery number per thread\n");
  return;
}

int
cmdline_parse(
              int argc,
              char **argv,
              NG_THD *thd)
{
    int opt, res = 0;
  /* Input Check */
  if (argc < 6)
  {
    my_help();
    return 1;
  }
  
  /* Parse command line */
  while (1)
  {
    opt = getopt(argc, argv, "c:t:u:p:d:h:P:");
    if ( opt == EOF) 
      break;
    
    switch (opt) {
    case 't':
      thd->thread = atoi(optarg);
      break;
    case 'h':
      memset(thd->host, 0, 0x2F);
      memcpy(thd->host, optarg, 0x2F);
      break;
    case 'P':
      thd->port = atoi(optarg);
      break;
    case 'u':
      memset(thd->user, 0, 0xF);
      memcpy(thd->user, optarg, 0xF);
      break;
    case 'p':
      memset(thd->pass, 0, 0x2F);
      memcpy(thd->pass, optarg, 0x2F);
      break;
    case 'd':
      memset(thd->db, 0, 0x2F);
      memcpy(thd->db, optarg, 0x2F);
      break;
    case 'c':
      thd->query_count = atoi(optarg);
      break;
    case '?':
      printf("Unknown options\n");
      my_help();
      res++;
      break;
    case ':':
      printf("Format error\n");
      my_help();
      res++;
      break;
    default:
      printf("International error.\n");
      res++;
    }
  }
  return res;
}
